package com.offer;

import com.study.common.ListNode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-12-05 15:27
 * @version: 1.0
 * @description: 从尾到头打印链表
 **/
public class NodeReversePrint {
    public int[] reversePrint(ListNode head) {
        if(head == null) return new int[0];
        Deque<Integer> stack = new ArrayDeque();
        int count=0;

        while (head != null){
            stack.push(head.value);
            head = head.next;
        }

        int[] arr = new int[stack.size()];
        while (!stack.isEmpty()){
            arr[count++] = stack.pop();
        }
        return arr;
    }

    public ListNode reverseList(ListNode head) {
        ListNode prev = null,next,curr = head;
        while (curr != null){
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}
